package models

///////////////////////命令记录/////////////////////////////

func AddCmdRecord(obj CmdRecord) {
	db.Create(&obj)
}

func GetCmdRecord(key string) (CmdRecord, error) {
	cmd := CmdRecord{}
	err := db.Where("cmd = ?", key).First(&cmd).Error
	if err != nil {
		return cmd, err
	}
	return cmd, nil
}

//更新
func UpdateCmdRecord(id int, cmd CmdRecord) {
	obj := CmdRecord{}
	db.First(&obj, id).Update(&cmd)
}

func SearchCmdRecord(key string) []CmdRecord {
	cmds := []CmdRecord{}
	db.Order("created_at desc").Order("count desc").Where("cmd like ?", "%"+key+"%").Find(&cmds)
	return cmds
}

//////////////////////脚本管理//////////////////////////////
//添加
func AddScript(obj OpsScript) {
	db.Create(&obj)
}

//查询ALL
func GetAllScript() []OpsScript {
	objs := []OpsScript{}
	db.Order("created_at desc").Find(&objs)
	return objs
}

//查询
func PageScript(page int) ([] OpsScript) {
	data_list := [] OpsScript{}
	db.Order("created_at desc").Limit(12).Offset(12 * (page - 1)).Find(&data_list)
	return data_list
}

//更新
func UpScript(id int, script OpsScript) {
	obj := OpsScript{}
	db.First(&obj, id).Update(&script)
}

//ID查询
func GetScriptId(id int) OpsScript {
	obj := OpsScript{}
	db.First(&obj, id)
	return obj
}

//删除
func DelScript(id int) {
	obj := OpsScript{}
	db.First(&obj, id).Delete(&obj)
}
